/* 
1 登录
  1 给登录表单绑定提交事件
  2 阻止默认行为
  3 获取表单数据
  4 合法性验证
  5 发送请求 完成登录
  6 给出友好提示
  7 存token到本地存储中
  8 延迟跳转 首页 
 */

const form = document.querySelector("form");
console.log(form);

form.addEventListener("submit", async function (e) {
  e.preventDefault();
  const data = serialize(form, { hash: true, empty: true });
  const res = await axios({
    url: "/login",
    method: "post",
    data: {
      ...data,
    },
  });
  console.log(res);
  if (res.request.status == 200) {
    localStorage.setItem("ttoken", res.data.data.token);
    document.querySelector("#myToast .toast-body").innerHTML = res.data.message;
    myToast.show();
    // 跳转页面
    setTimeout(() => {
      location.href = "index.html";
    }, 1500);
  }
});
